<template>
  <view class="base-card" :class="props.boxShadow ? 'box-shadow' : ''">
    <view class="base-card__title" v-if="props.title">
      <text>{{ props.title }}</text>
      <view
        class="base-card__title--expand"
        v-if="props.expanded"
        @click="() => (isExpanded = !isExpanded)"
      >
        <view class="label">
          <text v-if="isExpanded">收起</text>
          <template v-else>
            <view class="label-btn" v-if="props.expandedType === 'button'">{{
              props.expandedName
            }}</view>
            <text v-else>{{ props.expandedName }}</text>
          </template>
        </view>
        <IconFont
          v-if="props.expandedType === 'text' || isExpanded"
          :name="isExpanded ? 'rect-down' : 'rect-right'"
          size="14"
          color="#999"
        ></IconFont>
      </view>

      <view class="base-card__title--slot" v-if="$slots.titleRight">
        <slot name="titleRight" />
      </view>
    </view>

    <view class="base-card__content" v-if="isExpanded">
      <slot />
    </view>
  </view>
</template>

<script setup>
const props = defineProps({
  title: String,
  boxShadow: { type: Boolean, default: true },
  expanded: { type: Boolean, default: false },
  isDefaultExpanded: { type: Boolean, default: true },
  expandedType: { type: String, default: "text" }, // button / text
  expandedName: { type: String, default: "展开" },
});

const isExpanded = ref(props.isDefaultExpanded);
</script>

<style lang="scss">
.base-card {
  padding: 16px;
  border-radius: 8px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  width: 100%;
  box-sizing: border-box;
  margin-top: 12px;

  &.box-shadow {
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
  }

  &__title {
    font-size: 16px;
    color: #333;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;

    &::before {
      content: "";
      position: absolute;
      left: -16px;
      top: 5px;
      height: 14px;
      width: 4px;
      border-radius: 2px;
      background-color: #4c7bfd;
    }

    &--expand,
    &--slot {
      display: flex;
      font-size: 12px;
      color: #999;
    }

    .label-btn {
      color: #fff;
      background-color: #4c7bfd;
      border-radius: 4px;
      padding: 4px 8px;
    }
  }

  &__content {
    margin-top: 14px;
    flex: 1 1 auto;
  }
}
</style>
